﻿using Magicodes.ExporterAndImporter.Core.Extension;
using NP.BPMReportPlatform.DBHelper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace NP.BPMReportPlatform.Reports.CommonApprovalProcessReport
{
    public class CommonApprovalSqlFuncExtension
    {
        public CommonApprovalSqlFuncExtension()
        {
            //Console.WriteLine("CommonApprovalSqlFuncExtension 构造了");
        }

        public List<SqlFuncExternal> ExpandSqlFuncs = new List<SqlFuncExternal>()
        {
            new SqlFuncExternal()
            {
                UniqueMethodName = "ReplaceContent",
                MethodValue = (expInfo, dbType, expContext) =>
                {
                    if (dbType == DbType.MySql)
                    {
                        var sql = @" REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
                                REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
                                    REPLACE(REPLACE(REPLACE(REPLACE(""{0}"",
""[@:Text1]"",IFNULL(`{1}`.`Text1`,"""")),""[@:Text2]"",IFNULL(`{1}`.`Text2`,"""")),""[@:Text3]"",IFNULL(`{1}`.`Text3`,"""")),""[@:Text4]"",IFNULL(`{1}`.`Text4`,"""")),""[@:Text5]"",IFNULL(`{1}`.`Text5`,"""")),
""[@:Text6]"",IFNULL(`{1}`.`Text6`,"""")),""[@:Text7]"",IFNULL(`{1}`.`Text7`,"""")),""[@:Text8]"",IFNULL(`{1}`.`Text8`,"""")),""[@:Text9]"",IFNULL(`{1}`.`Text9`,"""")),""[@:Text10]"",IFNULL(`{1}`.`Text10`,"""")),
""[@:Text11]"",IFNULL(`{1}`.`Text11`,"""")),""[@:Text12]"",IFNULL(`{1}`.`Text12`,"""")),""[@:Text13]"",IFNULL(`{1}`.`Text13`,"""")),""[@:Text14]"",IFNULL(`{1}`.`Text14`,"""")),""[@:Text15]"",
IFNULL(`{1}`.`Text15`,"""")),""[@:Text16]"",IFNULL(`{1}`.`Text16`,"""")),""[@:TextArea1]"",IFNULL(`{1}`.`TextArea1`,"""")),""[@:TextArea2]"",IFNULL(`{1}`.`TextArea2`,"""")),""[@:TextArea3]"",
IFNULL(`{1}`.`TextArea3`,"""")),""[@:TextArea4]"",IFNULL(`{1}`.`TextArea4`,""""))  ";

                        return string.Format(sql, expInfo.Args[0].MemberValue, expInfo.Args[1].MemberValue);
                    }
                    else
                    {
                        throw new Exception("未实现");
                    }
                }
            },
            new SqlFuncExternal()
            {
                UniqueMethodName = "CustomFiledDisplay",
                MethodValue = (expInfo, dbType, expContext) =>
                {
                    if (dbType == DbType.MySql)
                    {
                        var sql = string.IsNullOrWhiteSpace(expInfo.Args[0].MemberValue?.ToString()) ? @" {1} " : @" {0} ";

                        return string.Format(sql, expInfo.Args[0].MemberValue, expInfo.Args[1].MemberValue);
                    }
                    else
                    {
                        throw new Exception("未实现");
                    }
                }
            }
        };

        public static string ReplaceContent(string combineContent, string tableName)
            => throw new NotSupportedException("Can only be used in expressions");

        public static string CustomFiledDisplay(string combineContent, string field)
            => throw new NotSupportedException("Can only be used in expressions");
    }
}
